package com.y.utils;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.y.entity.vo.HashPassword;

/**
 * @author: Yjh
 */
public class GeneratorHashPassword {

    public static HashPassword createHashPassword(String password){
        // 生成随机盐
        String salt = RandomUtil.randomString(8);
        //  md5加密2次
        String hashPassword = SecureUtil.md5(SecureUtil.md5(password + salt));
        return new HashPassword(hashPassword,salt);
    }

    /**
     * 检查密码
     * @param checkPassword 要检查的密码
     * @param salt  原 salt
     * @param originalPassword  原密码
     * @return
     */
    public static boolean checkHashPassword(String checkPassword,String salt,String originalPassword){
        //  加密
        String hashPassword = SecureUtil.md5(SecureUtil.md5(checkPassword + salt));
        return originalPassword.equals(hashPassword) ;
    }

    public static void main(String[] args) {

        HashPassword hashPassword = createHashPassword("123456");
        System.out.println(hashPassword);

    }


}
